﻿package com.amcharts.chartClasses
{
    import com.amcharts.axes.*;
    import com.amcharts.events.*;
    import com.amcharts.utils.*;
    import flash.display.*;
    import flash.events.*;
    import mx.collections.*;
    import mx.core.*;
    import mx.effects.easing.*;
    import mx.formatters.*;
    import mx.styles.*;
    import mx.utils.*;

    public class AmCoordinateChart extends AmChart
    {
        protected var _labelsContainer:Sprite;
        protected var _graphs:Array;
        protected var _valueAxesContainer:UIComponent;
        protected var _dataParser:DataParser;
        protected var _chartCursor:ChartCursor;
        protected var _categoryField:String;
        protected var _grid0:Sprite;
        protected var _graphCounter:Number;
        protected var _valueAxes:Array;
        protected var _chartType:String;
        protected var _valuesContainer:Sprite;
        protected var _axisCounter:Number;
        protected var _graphsContainer:UIComponent;
        protected var _urlTarget:String = "_self";
        static const VERSION:String = "1.8.3.1";
        private static var stylesInited:Boolean = initStyles();

        public function AmCoordinateChart()
        {
            _valueAxes = new Array();
            _graphs = new Array();
            _dataParser = new DataParser();
            _graphCounter = -1;
            _axisCounter = -1;
            return;
        }// end function

        public function highlightGraph(param1:AmGraph) : void
        {
            var _loc_3:Number = NaN;
            var _loc_5:AmGraph = null;
            var _loc_2:* = _graphs.length;
            var _loc_4:Number = 0.05;
            if (_legend)
            {
                _loc_4 = _legend.getStyle("rollOverGraphAlpha");
            }
            _loc_3 = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_5 = _graphs[_loc_3];
                if (_loc_5 != param1)
                {
                    _loc_5.alpha = _loc_4;
                }
                _loc_3 = _loc_3 + 1;
            }
            return;
        }// end function

        public function showGraphsBalloon(param1:AmGraph) : void
        {
            param1.showBalloon = true;
            updateLegend();
            return;
        }// end function

        public function removeValueAxis(param1:ValueAxis) : void
        {
            var _loc_4:AmGraph = null;
            var _loc_2:* = _graphs.length;
            var _loc_3:* = _loc_2 - 1;
            while (_loc_3 >= 0)
            {
                
                _loc_4 = _graphs[_loc_3];
                if (_loc_4)
                {
                    if (_loc_4.valueAxis == param1)
                    {
                        removeGraph(_loc_4);
                    }
                }
                _loc_3 = _loc_3 - 1;
            }
            _loc_2 = _valueAxes.length;
            _loc_3 = _loc_2 - 1;
            while (_loc_3 >= 0)
            {
                
                if (_valueAxes[_loc_3] == param1)
                {
                    _valueAxes.splice(_loc_3, 1);
                }
                _loc_3 = _loc_3 - 1;
            }
            param1.destroy();
            if (_valueAxesContainer == param1.parent)
            {
                _valueAxesContainer.removeChild(param1);
            }
            param1 = null;
            _dataChanged = true;
            invalidateProperties();
            return;
        }// end function

        public function addValueAxis(param1:ValueAxis) : ValueAxis
        {
            _valueAxes.push(param1);
            _dataChanged = true;
            invalidateProperties();
            return param1;
        }// end function

        protected function processValueAxis(param1:ValueAxis, param2:Number) : void
        {
            setAxisRenderers(param1);
            param1.valuesContainer = _valuesContainer;
            if (!param1.id)
            {
                _axisCounter = Utils.chooseId(_valueAxes, _axisCounter, "valueAxis", "id");
                param1.id = "valueAxis" + _axisCounter;
            }
            param1.chart = this;
            param1.grid0 = _grid0;
            param1.usePrefixes = _usePrefixes;
            return;
        }// end function

        override protected function updateData() : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:ValueAxis = null;
            Utils.removeChildren(_grid0);
            var _loc_1:* = _valueAxes.length;
            if (_loc_1 < 1)
            {
                _loc_3 = new ValueAxis();
                addValueAxis(_loc_3);
            }
            _loc_1 = _valueAxes.length;
            _loc_2 = 0;
            while (_loc_2 < _loc_1)
            {
                
                processValueAxis(_valueAxes[_loc_2], _loc_2);
                _loc_2 = _loc_2 + 1;
            }
            if (_graphs.length < 1)
            {
                showError("No graphs were added");
            }
            _loc_1 = _graphs.length;
            _loc_2 = 0;
            while (_loc_2 < _loc_1)
            {
                
                processGraph(_graphs[_loc_2], _loc_2);
                _loc_2 = _loc_2 + 1;
            }
            return;
        }// end function

        public function getGraphById(param1:String) : AmGraph
        {
            var _loc_2:AmGraph = null;
            var _loc_5:AmGraph = null;
            var _loc_3:* = _graphs.length;
            var _loc_4:Number = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = _graphs[_loc_4];
                if (_loc_5.id == param1)
                {
                    _loc_2 = _loc_5;
                }
                _loc_4 = _loc_4 + 1;
            }
            return _loc_2;
        }// end function

        override protected function createChildren() : void
        {
            super.createChildren();
            _valuesContainer = new Sprite();
            this.addChild(_valuesContainer);
            _labelsContainer = new Sprite();
            this.addChild(_labelsContainer);
            _valueAxesContainer = new UIComponent();
            this.addChild(_valueAxesContainer);
            _graphsContainer = new UIComponent();
            this.addChild(_graphsContainer);
            _grid0 = new Sprite();
            this.addChild(_grid0);
            return;
        }// end function

        protected function processGraph(param1:AmGraph, param2:Number) : void
        {
            if (!param1.id)
            {
                _graphCounter = Utils.chooseId(_graphs, _graphCounter, "graph", "id");
                param1.id = "graph" + _graphCounter;
            }
            if (!param1.valueAxis)
            {
                param1.valueAxis = _valueAxes[0];
            }
            param1.removeEventListener(GraphEvent.ROLL_OUT_GRAPH_ITEM, handleGraphEvents);
            param1.removeEventListener(GraphEvent.ROLL_OVER_GRAPH_ITEM, handleGraphEvents);
            param1.removeEventListener(GraphEvent.CLICK_GRAPH_ITEM, handleGraphEvents);
            param1.removeEventListener(GraphEvent.DOUBLE_CLICK_GRAPH_ITEM, handleGraphEvents);
            param1.addEventListener(GraphEvent.ROLL_OUT_GRAPH_ITEM, handleGraphEvents);
            param1.addEventListener(GraphEvent.ROLL_OVER_GRAPH_ITEM, handleGraphEvents);
            param1.addEventListener(GraphEvent.CLICK_GRAPH_ITEM, handleGraphEvents);
            param1.addEventListener(GraphEvent.DOUBLE_CLICK_GRAPH_ITEM, handleGraphEvents);
            param1.chart = this;
            return;
        }// end function

        protected function chooseGraphColor(param1:AmGraph, param2:Number) : void
        {
            var _loc_3:Array = null;
            var _loc_4:Number = NaN;
            if (isNaN(param1.getStyle("lineColor")))
            {
                _loc_3 = this.getStyle("colors");
                if (_loc_3.length > param2)
                {
                    _loc_4 = _loc_3[param2];
                }
                else
                {
                    _loc_4 = Math.round(Math.random() * 16777215);
                }
                param1.setStyle("lineColor", _loc_4);
            }
            return;
        }// end function

        public function removeGraph(param1:AmGraph) : void
        {
            param1.destroy();
            var _loc_2:* = _graphs.length;
            var _loc_3:* = _loc_2 - 1;
            while (_loc_3 >= 0)
            {
                
                if (_graphs[_loc_3] == param1)
                {
                    _graphs.splice(_loc_3, 1);
                }
                _loc_3 = _loc_3 - 1;
            }
            if (_graphsContainer == param1.parent)
            {
                _graphsContainer.removeChild(param1);
            }
            _dataChanged = true;
            invalidateProperties();
            return;
        }// end function

        protected function setAxisRenderers(param1:AxisBase) : void
        {
            return;
        }// end function

        public function get valueAxes() : Array
        {
            return _valueAxes;
        }// end function

        public function hideGraph(param1:AmGraph) : void
        {
            param1.hidden = true;
            _dataChanged = true;
            invalidateProperties();
            return;
        }// end function

        public function addGraph(param1:AmGraph) : AmGraph
        {
            _graphs.push(param1);
            _dataChanged = true;
            invalidateProperties();
            return param1;
        }// end function

        public function get chartType() : String
        {
            return _chartType;
        }// end function

        override public function styleChanged(param1:String) : void
        {
            super.styleChanged(param1);
            if (param1)
            {
                if (ArrayUtil.getItemIndex(param1, ["startAlpha", "startDuration", "startEffect", "sequencedAnimation"]) != -1)
                {
                    animateAgain();
                }
                invalidateDisplayList();
                return;
            }
            return;
        }// end function

        override protected function handleLegendEvent(event:LegendEvent) : void
        {
            var _loc_2:* = event.type;
            var _loc_3:* = event.index;
            var _loc_4:* = _graphs[_loc_3] as AmGraph;
            var _loc_5:* = _loc_4.hidden;
            var _loc_6:* = _loc_4.showBalloon;
            switch(_loc_2)
            {
                case LegendEvent.CLICK_MARKER:
                {
                    if (_loc_6)
                    {
                        hideGraphsBalloon(_loc_4);
                    }
                    else
                    {
                        showGraphsBalloon(_loc_4);
                    }
                    break;
                }
                case LegendEvent.CLICK_LABEL:
                {
                    if (_loc_6)
                    {
                        hideGraphsBalloon(_loc_4);
                    }
                    else
                    {
                        showGraphsBalloon(_loc_4);
                    }
                    break;
                }
                case LegendEvent.ROLL_OVER_ITEM:
                {
                    if (!_loc_5)
                    {
                        highlightGraph(_loc_4);
                    }
                    break;
                }
                case LegendEvent.ROLL_OUT_ITEM:
                {
                    if (!_loc_5)
                    {
                        unhighlightGraph();
                    }
                    break;
                }
                case LegendEvent.HIDE_ITEM:
                {
                    hideGraph(_loc_4);
                    break;
                }
                case LegendEvent.SHOW_ITEM:
                {
                    showGraph(_loc_4);
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        protected function handleGraphEvents(event:GraphEvent) : void
        {
            var _loc_4:String = null;
            var _loc_5:AmGraph = null;
            var _loc_6:String = null;
            var _loc_7:GraphDataItem = null;
            var _loc_8:Number = NaN;
            var _loc_9:Array = null;
            var _loc_10:NumberFormatter = null;
            var _loc_11:String = null;
            var _loc_12:Number = NaN;
            var _loc_13:Array = null;
            var _loc_2:* = new GraphEvent(event.type);
            _loc_2.graph = event.graph;
            _loc_2.item = event.item;
            _loc_2.index = event.index;
            dispatchEvent(_loc_2);
            var _loc_3:* = event.graph.showBalloon;
            if (_chartCursor)
            {
                if (_chartCursor.valueBalloonsEnabled)
                {
                    _loc_3 = false;
                }
            }
            if (_loc_3)
            {
                _loc_4 = event.type;
                switch(_loc_4)
                {
                    case GraphEvent.ROLL_OVER_GRAPH_ITEM:
                    {
                        _loc_5 = event.graph;
                        _loc_6 = _loc_5.balloonText;
                        if (_loc_6)
                        {
                            _loc_7 = event.item;
                            _balloon.follow = true;
                            _loc_8 = _loc_5.getStyle("lineColor");
                            _loc_9 = _loc_5.getStyle("fillColors");
                            if (_loc_9)
                            {
                                _loc_8 = _loc_9[0];
                            }
                            if (!isNaN(_loc_5.getStyle("balloonColor")))
                            {
                                _loc_8 = _loc_5.getStyle("balloonColor");
                            }
                            if (_loc_7.hasOwnProperty("isNegative"))
                            {
                                if (_loc_7.isNegative)
                                {
                                    _loc_12 = _loc_5.getStyle("negativeLineColor");
                                    _loc_13 = _loc_5.getStyle("negativeFillColors");
                                    if (_loc_13)
                                    {
                                        _loc_12 = _loc_13[0];
                                    }
                                    if (!isNaN(_loc_12))
                                    {
                                        _loc_8 = _loc_12;
                                    }
                                }
                            }
                            if (!isNaN(event.item.color))
                            {
                                _loc_8 = event.item.color;
                            }
                            _balloon.changeColor(ColorUtil.adjustBrightness(_loc_8, -25));
                            _loc_10 = _loc_5.numberFormatter;
                            if (!_loc_10)
                            {
                                _loc_10 = _numberFormatter;
                            }
                            _loc_11 = Utils.formatString(_loc_6, _loc_7.values, _loc_10, _percentFormatter);
                            _loc_11 = Utils.formatString(_loc_11, _loc_7, _loc_10, _percentFormatter);
                            if (_loc_11)
                            {
                            }
                            if (_loc_11 != "")
                            {
                                _balloon.show(_loc_11);
                            }
                        }
                        break;
                    }
                    case GraphEvent.ROLL_OUT_GRAPH_ITEM:
                    {
                        _balloon.hide();
                        break;
                    }
                    case GraphEvent.CLICK_GRAPH_ITEM:
                    {
                        _balloon.hide();
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            return;
        }// end function

        public function set urlTarget(param1:String) : void
        {
            _urlTarget = param1;
            return;
        }// end function

        protected function createChart() : void
        {
            return;
        }// end function

        public function updateLegendStyles() : void
        {
            dispatchEvent(new Event("legendStylesChanged"));
            return;
        }// end function

        public function get labelsContainer() : Sprite
        {
            return _labelsContainer;
        }// end function

        public function unhighlightGraph() : void
        {
            var _loc_3:AmGraph = null;
            var _loc_1:* = _graphs.length;
            var _loc_2:Number = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = _graphs[_loc_2];
                _loc_3.alpha = 1;
                _loc_2 = _loc_2 + 1;
            }
            return;
        }// end function

        public function showGraph(param1:AmGraph) : void
        {
            param1.hidden = false;
            _dataChanged = true;
            invalidateProperties();
            return;
        }// end function

        public function set graphs(param1:Array) : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:AmGraph = null;
            if (_graphs.length > 0)
            {
                _loc_2 = _graphs.length - 1;
                while (_loc_2 >= 0)
                {
                    
                    removeGraph(_graphs[_loc_2]);
                    _loc_2 = _loc_2 - 1;
                }
            }
            Utils.removeChildren(_graphsContainer);
            _graphs = new Array();
            if (param1)
            {
                _loc_2 = 0;
                while (_loc_2 < param1.length)
                {
                    
                    _loc_3 = param1[_loc_2];
                    _graphs.push(_loc_3);
                    _loc_2 = _loc_2 + 1;
                }
            }
            invalidateProperties();
            return;
        }// end function

        public function get urlTarget() : String
        {
            return _urlTarget;
        }// end function

        public function get graphs() : Array
        {
            return _graphs;
        }// end function

        override public function get legendDataProvider() : ArrayCollection
        {
            var _loc_1:* = new ArrayCollection();
            _loc_1.source = _graphs;
            return _loc_1;
        }// end function

        public function set valueAxes(param1:Array) : void
        {
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            var _loc_4:ValueAxis = null;
            if (param1)
            {
                _loc_2 = _valueAxes.length;
                _loc_3 = _loc_2 - 1;
                while (_loc_3 >= 0)
                {
                    
                    removeValueAxis(_valueAxes[_loc_3]);
                    _loc_3 = _loc_3 - 1;
                }
                _valueAxes = new Array();
                _loc_3 = 0;
                while (_loc_3 < param1.length)
                {
                    
                    _loc_4 = param1[_loc_3];
                    _valueAxes.push(_loc_4);
                    _loc_3 = _loc_3 + 1;
                }
                invalidateProperties();
            }
            return;
        }// end function

        public function hideGraphsBalloon(param1:AmGraph) : void
        {
            param1.showBalloon = false;
            updateLegend();
            return;
        }// end function

        public function getValueAxisById(param1:String) : ValueAxis
        {
            var _loc_3:ValueAxis = null;
            var _loc_2:* = _valueAxes.length;
            var _loc_4:Number = 0;
            while (_loc_4 < _loc_2)
            {
                
                if (_valueAxes[_loc_4].id == param1)
                {
                    _loc_3 = _valueAxes[_loc_4];
                    ;
                }
                _loc_4 = _loc_4 + 1;
            }
            return _loc_3;
        }// end function

        override protected function updateDisplayList(param1:Number, param2:Number) : void
        {
            super.updateDisplayList(param1, param2);
            destroy();
            if (this.height)
            {
            }
            if (this.width)
            {
                if (_chartDataProvider.length > 0)
                {
                    createChart();
                }
                else
                {
                    showError("no data found");
                }
            }
            return;
        }// end function

        protected function destroy() : void
        {
            var _loc_3:ValueAxis = null;
            var _loc_4:AmGraph = null;
            var _loc_1:* = _valueAxes.length;
            var _loc_2:Number = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_3 = _valueAxes[_loc_2];
                _loc_3.clean();
                _loc_2 = _loc_2 + 1;
            }
            _loc_1 = _graphs.length;
            _loc_2 = 0;
            while (_loc_2 < _loc_1)
            {
                
                _loc_4 = _graphs[_loc_2];
                _loc_4.clean();
                _loc_2 = _loc_2 + 1;
            }
            Utils.removeChildren(_graphsContainer);
            Utils.removeChildren(_valueAxesContainer);
            return;
        }// end function

        public function animateAgain() : void
        {
            var _loc_1:* = _graphs.length;
            var _loc_2:Number = 0;
            while (_loc_2 < _loc_1)
            {
                
                _graphs[_loc_2].animationPlayed = false;
                _loc_2 = _loc_2 + 1;
            }
            invalidateDisplayList();
            return;
        }// end function

        private static function initStyles() : Boolean
        {
            var styleDeclaration:* = StyleManager.getStyleDeclaration("AmCoordinateChart");
            if (!styleDeclaration)
            {
                styleDeclaration = new CSSStyleDeclaration();
            }
            styleDeclaration.defaultFactory = function () : void
            {
                this.plotAreaFillColors = [16777215];
                this.plotAreaFillAlphas = [1];
                this.plotAreaBorderColor = 0;
                this.plotAreaBorderAlpha = 0;
                this.startAlpha = 0;
                this.startDuration = 0;
                this.startFrom = "top";
                this.startEffect = Exponential.easeOut;
                this.sequencedAnimation = true;
                this.colors = [16737792, 16568834, 11591177, 888527, 2755792, 13438324, 13369344, 52224, 204, 14540253, 10066329, 3355443, 10027008];
                return;
            }// end function
            ;
            StyleManager.setStyleDeclaration("AmCoordinateChart", styleDeclaration, false);
            return true;
        }// end function

    }
}
